/*###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############*/


var JFXWidthSlideMode = '%';

$(document).ready(function(){
    JFXLeftColHide();
    $('#jfx-width-slider').slider({
        value : 100,
        slide : function(event, ui){
            var width, total, margins, marginLeft, marginRight, newWidth;
            if(JFXWidthSlideMode == '%'){
                // percentage mode
                width = ui.value;
                total = 100;
                margins = total - width;
                marginLeft = Math.round(margins/2);
                marginRight = margins-marginLeft;
                if(marginRight > marginLeft){
                    marginRight -= 0.5;
                    marginLeft += 0.5;
                }else if(marginLeft > marginRight){
                    marginLeft -= 0.5;
                    marginRight += 0.5;
                }
                $('#jfx-page-workspace-inner').css('width', width+'%');
                $('#jfx-page-workspace-inner').css('margin-left', marginLeft+'%');
                $('#jfx-page-workspace-inner').css('margin-right', marginRight+'%');
                $('#jfx-width-text').attr('value', width);
            }else if(JFXWidthSlideMode == 'px'){
                // pixel mode
                width = ui.value;
                total = document.body.clientWidth;
                // work out our width from % slider basis..
                newWidth = (total/100)*width;
                width = Math.round(newWidth);
                margins = total - width;
                marginLeft = Math.round(margins/2);
                marginRight = margins-marginLeft;
                $('#jfx-page-workspace-inner').css('width', width+'px');
                $('#jfx-page-workspace-inner').css('margin-left', marginLeft+'px');
                $('#jfx-page-workspace-inner').css('margin-right', marginRight+'px');
                $('#jfx-width-text').attr('value', width);
            }
        }
    });


    $('#jfx-width-mode input[type=radio]').change(function(){
        if(this.value == 'pc') JFXWidthSlideMode = '%';
        else JFXWidthSlideMode = 'px';
    });


    $('#jfx-width-text').keyup(function(){
        var width, total, margins, marginLeft, marginRight, newWidth;
        if(JFXWidthSlideMode == '%'){
            // percentage mode
            width = this.value*1;
            total = 100;
            margins = total - width;
            marginLeft = Math.round(margins/2);
            marginRight = margins-marginLeft;
            if(marginRight > marginLeft){
                marginRight -= 0.5;
                marginLeft += 0.5;
            }else if(marginLeft > marginRight){
                marginLeft -= 0.5;
                marginRight += 0.5;
            }

            $('#jfx-page-workspace-inner').css('width', width+'%');
            $('#jfx-page-workspace-inner').css('margin-left', marginLeft+'%');
            $('#jfx-page-workspace-inner').css('margin-right', marginRight+'%');
        }else if(JFXWidthSlideMode == 'px'){
            // pixel mode
            width = this.value*1;
            total = document.body.clientWidth;
            margins = total - width;
            marginLeft = Math.round(margins/2);
            marginRight = margins-marginLeft;
            $('#jfx-page-workspace-inner').css('width', width+'px');
            $('#jfx-page-workspace-inner').css('margin-left', marginLeft+'px');
            $('#jfx-page-workspace-inner').css('margin-right', marginRight+'px');
        }

    });
});

